/**
 * @api post control.
 *
 * User: yunshengzhu
 * Date: 2020/12/3
 * Time: 下午2:19
 */
package control

import (
	"card-platform/config"
	"card-platform/model"
	"encoding/json"
	"errors"
	"pkg/cfg"
	"time"
)

type Pro struct {
}

// swagger:operation POST /createPro 商品类 createProReq
// ---
// summary: 添加商品 createPro.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/okResp"
func (a *Pro) CreatePro(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	req := CreateProReq{}
	err = json.Unmarshal(data, &req)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR_JSON, err
	}

	merchant, err := model.GetMerchantUserId(userId)
	if err != nil && err != model.ErrRecordNotFound {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}

	if merchant.Mid <= 0 {
		return nil, config.ERR, errors.New("当前用户还未成为商家")
	}

	pro := model.ProDb{}
	pro.Mid = merchant.Mid
	pro.CreateTime = time.Now().Unix()
	pro.Price = req.Price
	imgBytes, _ := json.Marshal(req.Img)
	pro.Img = string(imgBytes)
	pro.Content = req.Content
	pro.ProName = req.ProName
	pro.Online = req.Online
	pro.Status = 0
	pro.Excuse = ""
	err = model.CreatePro(pro)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}
	return nil, config.OK, nil
}

// swagger:operation POST /delPro 商品类 delProReq
// ---
// summary: 删除商品 delPro.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/okResp"
func (a *Pro) DelPro(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	req := DelProReq{}
	err = json.Unmarshal(data, &req)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR_JSON, err
	}

	merchant, err := model.GetMerchantUserId(userId)
	if err != nil && err != model.ErrRecordNotFound {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}

	if merchant.Mid <= 0 {
		return nil, config.ERR, errors.New("当前用户还未成为商家")
	}

	pro, _ := model.GetProSku(req.Sku)
	if pro.Mid != merchant.Mid {
		return nil, config.ERR, errors.New("无数据权限")
	}

	err = model.DelPro(req.Sku)

	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}
	return nil, config.OK, nil
}

// swagger:operation POST /proDetails 商品类 proDetailsReq
// ---
// summary: 商品详情 proDetails.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/proResp"
func (a *Pro) ProDetails(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	req := ProDetailsReq{}
	err = json.Unmarshal(data, &req)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR_JSON, err
	}

	obj, err := model.GetProSku(req.Sku)
	if err != nil && err != model.ErrRecordNotFound {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}
	obj = reloadProDetails(obj)
	return obj, config.OK, nil
}

func reloadProDetails(pro model.ProDb) model.ProDb {
	err := json.Unmarshal([]byte(pro.Img), &pro.Imgs)
	if err != nil {
		cfg.LogErr("err:", err)
	}
	return pro
}

func reloadVProDetails(pro model.VProDb) model.VProDb {
	err := json.Unmarshal([]byte(pro.Img), &pro.Imgs)
	if err != nil {
		cfg.LogErr("err:", err)
	}
	return pro
}

// swagger:operation POST /proList 商品类 proListReq
// ---
// summary: 商品列表 proList.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/proListResp"
func (a *Pro) ProList(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	req := ProListReq{}
	err = json.Unmarshal(data, &req)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR_JSON, err
	}

	if req.Mid <= 0 {
		merchant, _ := model.GetMerchantUserId(userId)
		req.Mid = merchant.Mid
		user, _ := model.GetUserId(userId)
		if user.Platform == 1 {
			req.Mid = 0
		}
	}

	count, err := model.ProCount(req.Mid, req.Key, req.Status, req.Online)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}

	list, err := model.ProList(req.Mid, req.Key, req.Status, req.Online, req.Page, req.Size)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}

	for idx, v := range list {
		list[idx] = reloadProDetails(v)
	}
	o := make(map[string]interface{})
	o["count"] = count
	o["list"] = list

	return o, config.OK, nil
}

// swagger:operation POST /updatePro 商品类 updateProReq
// ---
// summary: 更新商品 updatePro.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/okResp"
func (a *Pro) UpdatePro(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	req := UpdateProReq{}
	err = json.Unmarshal(data, &req)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR_JSON, err
	}

	pro, err := model.GetProSku(req.Sku)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}

	if pro.Sku <= 0 {
		return nil, config.ERR, errors.New("商品不存在")
	}

	merchant, err := model.GetMerchantUserId(userId)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}
	if merchant.Mid <= 0 {
		return nil, config.ERR, errors.New("当前用户还未成为商家")
	}
	if pro.Mid != merchant.Mid {
		return nil, config.ERR, errors.New("不具备当前商品的编辑权限")
	}

	pro.Price = req.Price
	imgBytes, _ := json.Marshal(req.Img)
	pro.Img = string(imgBytes)
	pro.Content = req.Content
	pro.ProName = req.ProName
	pro.Online = req.Online
	pro.Status = 0
	pro.Excuse = ""
	err = model.UpdatePro(pro)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}
	return nil, config.OK, nil
}

// swagger:operation POST /proCheck 商品类 proCheckReq
// ---
// summary: 管理员审核商品 proCheck.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/okResp"
func (a *Pro) ProCheck(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	req := ProCheckReq{}
	err = json.Unmarshal(data, &req)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR_JSON, err
	}

	user, err := model.GetUserId(userId)
	if err != nil {
		cfg.LogErr("err:", err)
	}

	if user.Platform != 1 {
		cfg.LogErr("当前用不具备审核权限:", user)
		return nil, config.ERR, errors.New("当前用户不具备审核权限")
	}

	pro, err := model.GetProSku(req.Sku)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}

	if pro.Sku <= 0 {
		return nil, config.ERR, errors.New("商品不存在")
	}

	pro.Status = req.Status
	pro.Excuse = req.Excuse
	pro.CreateTime = time.Now().Unix()

	err = model.UpdatePro(pro)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}

	if pro.Status == 1 {
		//更新最低价格
		err := model.UpdateMerchantMinPrice(pro.Mid)
		if err != nil {
			cfg.LogErr("err:", err)
		}
	}

	return nil, config.OK, nil
}

// swagger:operation POST /pros 商品类 prosReq
// ---
// summary: 用户端商品列表 pros.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/prosResp"
func (a *Pro) Pros(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	req := ProsReq{}
	err = json.Unmarshal(data, &req)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR_JSON, err
	}
	resp := ProsResp{}
	resp.Count, err = model.GetVProCount(req.Online, req.Mid)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}

	resp.List, err = model.GetVProList(req.Online, req.Mid, req.Page, req.Size, req.Lon, req.Lat, req.Order)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR, err
	}

	for idx, v := range resp.List {
		resp.List[idx] = reloadVProDetails(v)
	}
	return resp, config.OK, nil
}
